#include "main.h"
void addFiles (char * fileNames[])
{
	FILE ** arc = NULL;
	FILE * fileToRead = NULL;
	char sign[] = _SIGN;
	char answer [256];
	char yes[] = "yes";
	char a[] = "XXXXXX";
	char *pos;
	char * arcnew = NULL;
	int i = 1;
	int rc = 0;
	int m = 0;
	struct stat * fileStat = (struct stat * ) calloc (1,sizeof(struct stat));
	fileHead * fileHd = NULL;
	fileHead ** list = NULL;
	if (!fileStat)	cl_exit (ADD_FILES,MEMORY_ERR);
	arcnew = (char * ) calloc (strlen(fileNames[0]) + 7,sizeof(char));
	envInf.tmpArcName = arcnew;
	if (!arcnew)	cl_exit (ADD_FILES,MEMORY_ERR);
	strcat (arcnew,fileNames[0]);	
	strcat (arcnew,a);
	arc = openArc (fileNames[0],arcnew);
	if (arc[0])		
		if ((rc = testing (arc[0])) == 1)	cl_exit (ADD_FILES,CS_ERR);
	if (!arc[0])	{
		if (arc[1])	{	
			if ((rc = fwrite (sign, sizeof(char), SIGN_LEN, arc[1])) != SIGN_LEN)	cl_exit (ADD_FILES,READ_ERR);
			while(fileNames[i])
			{
				fileHd = _makeFileHead(fileNames[i]);	
				envInf.fileHd = fileHd;	
				fileToRead = fopen (fileNames[i],"rb");
				envInf.fileToRead = fileToRead;
				if (fileToRead)	{
					rc = writeToArc (arc[1],fileToRead,fileHd);
					if (rc != 0)	cl_exit (ADD_FILES,WRITE_ERR);
					fclose (envInf.fileToRead);
					envInf.fileToRead = NULL;
					if (!(fileHd->huff_ubits & NO_HUFF))	{
						remove (envInf.tmpFileName);
						free(envInf.tmpFileName);
					}
					free (fileHd);
				} else	cl_exit (ADD_FILES, OPEN_FILE_ERR);
				i++;
			}
			fclose (arc[1]);	
			envInf.new_arc = NULL;
			clean_files (fileNames[0],arcnew,NEW_ARC_2);	
			return;
		}
	} else {	
		list = listingFile (arc[0]);
		while (fileNames[i])
		{
			pos = getName (fileNames[i]);	
			stat (fileNames[i],fileStat);
			m = 0;
			while (list[m])
			{
				if (!(strcmp (pos,list[m]->fileName)))
				{
					printf ( "Do you want replace:\n%s\nSize: %.3f Kb\nDate: %s\n"
							"To:\n%s\nSize: %.3f Kb\nDate: %s"
							"\n",pos,((float)list[m]->fileSize)/1024.0,ctime((time_t *)&list[m]->mtime),pos,fileStat->st_size / 1024.0,ctime(&fileStat->st_mtime));
					printf ("Input Yes or no: ");	scanf ("%s",answer);
					if (!(strnicmp(answer,yes,3)))	list[m]->rplFlag = REPL_FILE;
					else {
						list[m]->rplFlag = 0;	
					fileNames[i][0] = 0;
					}
				}
				m++;
			}
			i++;
		}
		copyArc (arc,list);
		i = 1;
		m = 0;
		while (list[m])
		{
			free(list[m]->fileName);
			free(list[m]);
			m++;
		}
		free(list);
		while (fileNames[i])
		{
			if (strlen(fileNames[i]) != 0)	{	
				fileHd = _makeFileHead(fileNames[i]);	
				envInf.fileHd = fileHd;
				fileToRead = fopen (fileNames[i],"rb");
				if (fileToRead)	{
					envInf.fileToRead = fileToRead;
					rc = writeToArc (arc[1],fileToRead,fileHd);
					if (rc != 0) 	cl_exit (ADD_FILES, WRITE_ERR);
					fclose (envInf.fileToRead);
					envInf.fileToRead = NULL;
						if (!(fileHd->huff_ubits & NO_HUFF))	{
						remove (envInf.tmpFileName);
						free(envInf.tmpFileName);
					}
					free (fileHd);
				} else 	cl_exit (ADD_FILES, OPEN_FILE_ERR);
			}
			i++;
		}
		free(fileStat);
		fclose (arc[0]);
		envInf.old_arc = NULL;
		fclose (arc[1]);
		envInf.new_arc = NULL;
		free (arc);
		rc = clean_files ( fileNames[0],arcnew,NEW_ARC);
		free (arcnew);
#ifdef __DEBUG_
		printf ("END\n");
#endif
	}
}